<?php
/**
 * TODO: Optimize the color names.
 */
const HTML_COLORS = [
    "纯黑色" => "#000000",
    "银灰色" => "#C0C0C0",
    "灰色" => "#808080",
    "纯白色" => "#FFFFFF",
    "栗色" => "#800000",
    "纯红色" => "#FF0000",
    "紫色" => "#800080",
    "紫红色" => "#FF00FF",
    "纯绿色" => "#008000",
    "闪光绿色" => "#00FF00",
    "橄榄色" => "#808000",
    "纯黄色" => "#FFFF00",
    "海军蓝色" => "#000080",
    "纯蓝色" => "#0000FF",
    "水鸭色" => "#008080",
    "青色" => "#00FFFF",
    "爱丽丝蓝色" => "#f0f8ff",
    "古董白色" => "#faebd7",
    "宝石碧绿色" => "#7fffd4",
    "蔚蓝色" => "#f0ffff",
    "米色" => "#f5f5dc",
    "陶坯黄色" => "#ffe4c4",
    "白杏色" => "#ffebcd",
    "蓝紫罗兰色" => "#8a2be2",
    "棕色" => "#a52a2a",
    "硬木色" => "#deb887",
    "军兰色" => "#5f9ea0",
    "黄绿色" => "#7fff00",
    "巧克力色" => "#d2691e",
    "珊瑚色" => "#ff7f50",
    "矢车菊蓝色" => "#6495ed",
    "玉米丝色" => "#fff8dc",
    "深红色" => "#dc143c",
    "青色" => "#00ffff",
    "暗蓝色" => "#00008b",
    "暗青色" => "#008b8b",
    "暗金菊黄色" => "#b8860b",
    "深灰色" => "#a9a9a9",
    "暗绿色" => "#006400",
    "暗黄褐色" => "#bdb76b",
    "深洋红色" => "#8b008b",
    "暗橄榄绿色" => "#556b2f",
    "深橙色" => "#ff8c00",
    "暗兰花紫色" => "#9932cc",
    "深红色" => "#8b0000",
    "鲑鱼色" => "#e9967a",
    "暗海洋绿色" => "#8fbc8f",
    "暗灰蓝色" => "#483d8b",
    "暗瓦灰色" => "#2f4f4f",
    "暗绿宝石色" => "#00ced1",
    "暗紫罗兰色" => "#9400d3",
    "深粉红色" => "#ff1493",
    "深天蓝色" => "#00bfff",
    "暗淡的灰色" => "#696969",
    "闪兰色" => "#1e90ff",
    "火砖色" => "#b22222",
    "花的白色" => "#fffaf0",
    "森林绿色" => "#228b22",
    "淡灰色" => "#dcdcdc",
    "幽灵白色" => "#f8f8ff",
    "金色" => "#ffd700",
    "金菊黄色" => "#daa520",
    "绿黄色" => "#adff2f",
    "绿黄色" => "#808080",
    "蜜瓜色" => "#f0fff0",
    "粉红色" => "#ff69b4",
    "印度红色" => "#cd5c5c",
    "靛青色" => "#4b0082",
    "象牙色" => "#fffff0",
    "黄褐色" => "#f0e68c",
    "淡紫色" => "#e6e6fa",
    "淡紫红色" => "#fff0f5",
    "草绿色" => "#7cfc00",
    "柠檬绸色" => "#fffacd",
    "亮蓝色" => "#add8e6",
    "淡珊瑚色" => "#f08080",
    "淡青色" => "#e0ffff",
    "亮菊黄色" => "#fafad2",
    "浅灰色" => "#d3d3d3",
    "淡绿色" => "#90ee90",
    "浅粉红色" => "#ffb6c1",
    "浅鲑鱼肉色" => "#ffa07a",
    "浅海洋绿色" => "#20b2aa",
    "亮天蓝色" => "#87cefa",
    "亮蓝灰色" => "#778899",
    "亮钢蓝色" => "#b0c4de",
    "浅黄色" => "#ffffe0",
    "闪光深绿色" => "#32cd32",
    "亚麻布色" => "#faf0e6",
    "玫瑰红色" => "#ff00ff",
    "中宝石碧绿色" => "#66cdaa",
    "中蓝色" => "#0000cd",
    "中兰花紫色" => "#ba55d3",
    "中紫色" => "#9370db",
    "中海洋绿色" => "#3cb371",
    "中暗蓝色" => "#7b68ee",
    "中春绿色" => "#00fa9a",
    "中绿宝石色" => "#48d1cc",
    "中紫罗兰红色" => "#c71585",
    "午夜蓝色" => "#191970",
    "薄荷奶油色" => "#f5fffa",
    "浅玫瑰色" => "#ffe4e1",
    "鹿皮色" => "#ffe4b5",
    "纳瓦白色" => "#ffdead",
    "老花色" => "#fdf5e6",
    "橄榄褐色" => "#6b8e23",
    "橙色" => "#ffa500",
    "橙红色" => "#ff4500",
    "暗紫色" => "#da70d6",
    "灰菊黄色" => "#eee8aa",
    "弱绿色" => "#98fb98",
    "弱绿宝石色" => "#afeeee",
    "弱紫罗兰红色" => "#db7093",
    "番木色" => "#ffefd5",
    "桃肉色" => "#ffdab9",
    "秘鲁色" => "#cd853f",
    "粉红色" => "#ffc0cb",
    "洋李色" => "#dda0dd",
    "粉蓝色" => "#b0e0e6",
    "玫瑰棕色" => "#bc8f8f",
    "皇家蓝色" => "#4169e1",
    "重褐色" => "#8b4513",
    "鲑鱼色" => "#fa8072",
    "沙棕色" => "#f4a460",
    "海洋绿色" => "#2e8b57",
    "海贝壳色" => "#fff5ee",
    "黄土赭色" => "#a0522d",
    "天蓝色" => "#87ceeb",
    "石蓝色" => "#6a5acd",
    "灰石色" => "#708090",
    "灰石色" => "#708090",
    "雪白色" => "#fffafa",
    "春绿色" => "#00ff7f",
    "铁青色" => "#4682b4",
    "茶色" => "#d2b48c",
    "蓟色" => "#d8bfd8",
    "番茄红色" => "#ff6347",
    "绿宝石色" => "#40e0d0",
    "紫罗兰色" => "#ee82ee",
    "浅黄色" => "#f5deb3",
    "白烟色" => "#f5f5f5",
    "黄绿色" => "#9acd32"        
];

function convertToColor(string $hex)
{
    if (!preg_match('/^#[[:xdigit:]]{3,6}$/', $hex)) {
        return $hex;
    }
    // Expand short 3 digit hex version.
    if (preg_match('/^#[[:xdigit:]]{3}$/', $hex)) {
        $new = '#';
        for ($i = 1; $i <= 3; $i++) {
            $new .= str_repeat($hex[$i], 2);
        }
        $hex = $new;
    }
    if (!preg_match('/^#[[:xdigit:]]{6}$/', $hex)) {
        return null;
    }
    // Find the best match in L1 distance.
    $bestmatch = '';
    $bestdist  = 999999;
    foreach (HTML_COLORS as $color => $rgb) {
        $dist = 0;
        for ($i = 1; $i <= 3; $i++) {
            sscanf(substr($hex, 2 * $i - 1, 2), '%x', $val1);
            sscanf(substr($rgb, 2 * $i - 1, 2), '%x', $val2);
            $dist += abs($val1 - $val2);
        }
        if ($dist < $bestdist) {
            $bestdist  = $dist;
            $bestmatch = $color;
        }
    }
    return $bestmatch;
}
